package com.maple.phoneward.service;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.maple.phoneward.activity.R;
import com.maple.phoneward.constants.Constants;
import com.maple.phoneward.utils.AppUtil;
import com.maple.phoneward.utils.Logger;
import com.maple.phoneward.utils.StringUtil;

/**
 * LocationService.java
 * 
 * @author YaJian Qiu
 * @time 2013-3-30 上午9:55:25<br>
 *       本类描述：位置服务
 * 
 */
public class LocationService extends Service {

	private static final String TAG = "LocationService";

	// 手机定位相关
	private LocationClient mLocClient;
	private MyLocationListenner mListener;

	class MyBind extends Binder {

	}

	@Override
	public IBinder onBind(Intent intent) {
		return new MyBind();
	}

	@Override
	public void onCreate() {
		Logger.d(TAG, "位置服务创建");
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		Logger.d(TAG, "在位置服务中执行命令");
		showLocation();
		return super.onStartCommand(intent, flags, startId);
	}

	@Override
	public void onDestroy() {
		Logger.d(TAG, "位置服务销毁");
	}

	/**
	 * 显示手机位置
	 */
	private void showLocation() {
		mLocClient = new LocationClient(this);
		mListener = new MyLocationListenner();
		mLocClient.registerLocationListener(mListener);
		// 设置相关参数
		LocationClientOption option = new LocationClientOption();
		option.setOpenGps(true); // 打开gps
		option.setCoorType("bd09ll"); // 设置坐标类型
		option.setServiceName("com.baidu.location.service_v2.9");
		option.setPoiExtraInfo(true);// 显示地址信息
		option.setAddrType("all");// 所有的信息
		option.setScanSpan(3000);
		option.setPriority(LocationClientOption.GpsFirst); // 不设置，默认是gps优先
		option.setPoiNumber(10);
		option.disableCache(true);
		mLocClient.setLocOption(option);
		mLocClient.start();
	}

	public class MyLocationListenner implements BDLocationListener {
		@Override
		public void onReceiveLocation(BDLocation location) {
			if (location == null)
				return;
			String addr = "";
			if (location.getLocType() == BDLocation.TypeNetWorkLocation) {
				addr = location.getAddrStr();
			}
			String locInfoTemp = String.format(AppUtil.getString(LocationService.this, R.string.loc_info_temp), addr, location.getLongitude(), location.getLatitude());
			Logger.d(TAG, locInfoTemp);
			if (mLocClient != null) {
				mLocClient.stop();
			}
			String num1 = AppUtil.getFromSharedPreferences(LocationService.this, Constants.CONFIG_SAFETY_PHONE_NUMBER_1);
			String num2 = AppUtil.getFromSharedPreferences(LocationService.this, Constants.CONFIG_SAFETY_PHONE_NUMBER_2);
			if (StringUtil.isNotEmpty(num1)) {
				AppUtil.sendMessage(num1, locInfoTemp);
			}
			if (StringUtil.isNotEmpty(num2)) {
				AppUtil.sendMessage(num2, locInfoTemp);
			}
			LocationService.this.stopSelf();
		}

		public void onReceivePoi(BDLocation poiLocation) {
			if (poiLocation == null) {
				return;
			}
		}
	}
}
